package com.kuiandjie.web;


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;
import com.kuiandjie.entity.Document;
import com.kuiandjie.entity.Employee;
import com.kuiandjie.entity.Log;
import com.kuiandjie.entity.Project;
import com.kuiandjie.service.DocumentService;
import com.kuiandjie.service.LogService;
import com.kuiandjie.util.DateUtil;

public class AddDocumentServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		if("POST".equals(request.getMethod())){
			HttpSession session = request.getSession();
			Project curp = (Project) session.getAttribute("curp");
			Employee cure = (Employee) session.getAttribute("employee");
			
			response.setContentType("text/html");
			PrintWriter out = response.getWriter();
			
			String title = request.getParameter("title");
			String content = request.getParameter("content");
			
			DocumentService ds = new DocumentService();
			Document d = new Document();
			d.setContent(content);
			d.setCreateTime(DateUtil.getNow());
			d.setEmpId(cure.getId());
			d.setEmpName(cure.getName());
			d.setProId(curp.getId());
			d.setTitle(title);
			
			int did = ds.saveDocument(d);
			
			Log log = new Log();
			log.setDesc("分享了资料<a href='editdocument.jspx?m=detail&did=" + did + "'>" + title + "</a>");
			log.setEmployeeId(cure.getId());
			log.setProjectId(curp.getId());
			log.setTime(DateUtil.getNow());
			log.setName(cure.getName());
			log.setPic(cure.getPic());
			
			LogService ls = new LogService();
			ls.inserLog(log);
			
			request.setAttribute("msg", new Gson().toJson(log));
			request.getRequestDispatcher("msgflow.jspx").include(request, response);
			out.print("1");
			out.flush();
			out.close();
			
		}else{
			request.getRequestDispatcher("WEB-INF/views/adddocument.jsp").forward(request, response);
		}
	}

}
